package org.abratuhi.kvngroupware.model;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import org.abratuhi.kvngroupware.gui.GUIBrainstorm;
import org.jdom.Element;

public class Brainstorm {
	
	String id = new String();
	String teamId = new String();
	String theme = new String();
	Integer round = 0;
	//ArrayList<Sheet> sheets = new ArrayList<Sheet>();
	ArrayList<String> sheets = new ArrayList<String>();
	ArrayList<User> members = new ArrayList<User>();
	ArrayList<String> stats = new ArrayList<String>();
	String rest = new String();
	Integer nCommits = 0;
	
	GUIBrainstorm gui;
		
	public Brainstorm(String id, String teamId, String theme){
		setId(id);
		setTeamId(teamId);
		setTheme(theme);
	}
	
	public String getId(){
		return this.id;
	}
	public String getTheme(){
		return this.theme;
	}
	public String getTeamId(){
		return this.teamId;
	}
	public ArrayList<String> getSheets(){
		return this.sheets;
	}
	public ArrayList<User> getMembers(){
		return this.members;
	}
	public ArrayList<String> getStats(){
		return this.stats;
	}
	public String[] getMemberIdsAsArray(){
		String[] ids = new String[getMembers().size()];
		for(int i=0; i<getMembers().size(); i++){
			ids[i] = getMembers().get(i).getId();
			ids[i]+= " ("+getStats().get(i)+")";
		}
		return ids;
	}
	public GUIBrainstorm getGUI(){
		return this.gui;
	}
	
	void setId(String id){
		this.id = id;
	}
	void setTheme(String theme){
		this.theme = theme;
	}
	void setTeamId(String teamId){
		this.teamId = teamId;
	}
	public void setGUI(GUIBrainstorm guibrainstorm){
		this.gui = guibrainstorm;
	}
	static String theme2id(String theme){
		return theme;
	}
	
	public synchronized String addMember(String userId){
		User u = new User(userId);
		return addMember(u);
	}
	public synchronized String addMember(User u){
		String returnMessage = "";
		if(this.getMembers().contains(u)){
			returnMessage = "Brainstorm already contains member with id:\t"+u.getId()+"."+"\n";
		}
		else{
			this.getMembers().add(u);
			this.getSheets().add(new String());
			this.getStats().add(new String());
		}
		return returnMessage;
	}
	public synchronized String addMember(User u, String sheet){
		String returnMessage = "";
		if(this.getMembers().contains(u)){
			returnMessage = "Brainstorm already contains member with id:\t"+u.getId()+"."+"\n";
		}
		else{
			this.getMembers().add(u);
			this.getSheets().add(new String(sheet));
			this.getStats().add(new String());
		}
		return returnMessage;
	}
	public synchronized String addMember(User u, String sheet, String status){
		String returnMessage = "";
		if(this.getMembers().contains(u)){
			returnMessage = "Brainstorm already contains member with id:\t"+u.getId()+"."+"\n";
		}
		else{
			this.getMembers().add(u);
			this.getSheets().add(new String(sheet));
			this.getStats().add(new String(status));
		}
		return returnMessage;
	}
	
	public synchronized String addMember(String uid, String sheet, String status){
		String returnMessage = "";
		if(this.getMembers().contains(uid)){
			returnMessage = "Brainstorm already contains member with id:\t"+uid+"."+"\n";
		}
		else{
			this.getMembers().add(new User(uid));
			this.getSheets().add(new String(sheet));
			this.getStats().add(new String(status));
		}
		return returnMessage;
	}
	
	public synchronized String removeMember(User u){
		return removeMember(u.getId());
	}
	public synchronized String removeMember(String id){
		int index = indexOf(id);
		String returnMessage = "";
		if(index == -1){
			returnMessage = "Brainstorm doesn't contain member with id:\t"+id+"."+"\n";
		}
		else{
			String text = this.getSheets().get(index);
			this.getMembers().remove(index);
			this.getSheets().remove(index);
			if(this.getSheets().size()>0) this.getSheets().get(0).concat("\n"+text);
			else this.rest = text;
		}
		return returnMessage;
	}
	
	public synchronized boolean containsMember(User u){
		return this.getMembers().contains(u);
	}
	public synchronized boolean containsMember(String id){
		for(int i=0; i<getMembers().size(); i++){
			if(getMembers().get(i).getId().equals(id)){
				return true;
			}
		}
		return false;
	}
	synchronized int indexOf(String id){
		for(int i=0; i<getMembers().size(); i++){
			if(getMembers().get(i).getId().equals(id)){
				return i;
			}
		}
		return -1;
	}
	
	public synchronized void commitSheet(String user, String sheet){
		getSheets().set(indexOf(user), sheet);
		setStat(user, "commit");
		nCommits ++;
	}
	
	public synchronized void updateSheet(String user, String sheet){
		getSheets().set(indexOf(user), sheet);
	}
	
	public String getStat(String id){
		return stats.get(indexOf(id));
	}
	public void setStat(String id, String status){
		stats.set(indexOf(id), status);
	}
	
	public synchronized void switchSheets(){
		for(int i=0; i<stats.size(); i++){
			stats.set(i, "write");
		}
		
		String s0 = getSheets().remove(0);
		getSheets().add(s0);
		round ++;
		nCommits = 0;
	}
	
	public synchronized String getSheet(String user){
		return getSheets().get(indexOf(user));
	}
	
	public String getSummary(){
		StringWriter sw = new StringWriter();
		sw.append(this.getId());
		sw.append("[");
		for(int i=0; i<members.size(); i++){
			sw.append(members.get(i).getId()+" ");
		}
		sw.append("]");
		return sw.toString();
	}
	
	public String getOverview(){
		StringWriter sw = new StringWriter();
		sw.append("Brainstorm ID:\t"+this.getId()+"\n\n");
		for(int i=0; i<sheets.size(); i++){
			sw.append("Sheet "+i+":\n");
			sw.append(sheets.get(i) + "\n");
		}
		return sw.toString();
	}
	
	public Element toElement(){
		Element brainstorm = new Element("brainstorm");
		brainstorm.setAttribute("id", getId());
		brainstorm.setAttribute("teamId", getTeamId());
		brainstorm.setAttribute("theme", getTheme());
		for(int i=0; i<getMembers().size(); i++){
			Element user = getMembers().get(i).toElement();
			user.setText(getSheets().get(i));
			user.setAttribute("status", stats.get(i));
			brainstorm.addContent(user);
		}
		return brainstorm;
	}
	
	public Element toBackupElement(){
		Element brainstorm = new Element("brainstorm");
		brainstorm.setText(rest);
		brainstorm.setAttribute("id", getId());
		brainstorm.setAttribute("teamId", getTeamId());
		brainstorm.setAttribute("theme", getTheme());
		return brainstorm;
	}
	
	@SuppressWarnings("unchecked")
	public static Brainstorm fromElement(Element eBrainstorm){
		Brainstorm brainstorm = new Brainstorm(eBrainstorm.getAttributeValue("id"),
				eBrainstorm.getAttributeValue("teamId"),
				eBrainstorm.getAttributeValue("theme"));
		List<Element> eUsers = eBrainstorm.getChildren();
		for(int i=0; i<eUsers.size(); i++){
			User user = User.fromElement(eUsers.get(i));
			String sheet = eUsers.get(i).getText();
			String status = eUsers.get(i).getAttributeValue("status");
			brainstorm.addMember(user, sheet, status);
		}
		return brainstorm;
	}

}
